package com.ssis.graphics;

import java.io.IOException;
import com.ssis.core.Game;

public class Starfield {
	private Game game;
	private static final String BIG_NAME = "starbig";
	private static final String MED_NAME = "starmed";
	private static final String SML_NAME = "starsml";
	
	private static final int BIG_STARS_COUNT = 5;
	private static final int BIG_STARS_ROWS = 2;
	private static final int BIG_STARS_COLS = 2;
	private static final int BIG_STARS_TYPES = BIG_STARS_ROWS * BIG_STARS_COLS;
	
	private static final int MED_STARS_COUNT = 10;
	private static final int MED_STARS_ROWS = 2;
	private static final int MED_STARS_COLS = 2;
	private static final int MED_STARS_TYPES = MED_STARS_ROWS * MED_STARS_COLS;
	
	private static final int SML_STARS_COUNT = 10;
	private static final int SML_STARS_ROWS = 2;
	private static final int SML_STARS_COLS = 2;
	private static final int SML_STARS_TYPES = SML_STARS_ROWS * SML_STARS_COLS;
	
	private Sprite[] big_stars;
	private Sprite[] med_stars;
	private Sprite[] sml_stars;
	
	private static final int speed = 1;
	
	public Starfield(Game game){
		this.game = game;

		big_stars = new Sprite[BIG_STARS_COUNT];
		med_stars = new Sprite[MED_STARS_COUNT];
		sml_stars = new Sprite[SML_STARS_COUNT];
		
		for(int i=0;i<BIG_STARS_COUNT;i++){
			big_stars[i]=new Sprite(
					BIG_NAME,
					game.rand().nextInt(Game.WIDTH),
					game.rand().nextInt(Game.HEIGHT),
					game.rand().nextInt(BIG_STARS_TYPES),
					BIG_STARS_ROWS, BIG_STARS_COLS);
		}		
		for(int i=0;i<MED_STARS_COUNT;i++){
			med_stars[i]=new Sprite(
					MED_NAME,
					game.rand().nextInt(Game.WIDTH),
					game.rand().nextInt(Game.HEIGHT),
					game.rand().nextInt(MED_STARS_TYPES),
					MED_STARS_ROWS, MED_STARS_COLS);
		}		
		for(int i=0;i<SML_STARS_COUNT;i++){
			sml_stars[i]=new Sprite(
					SML_NAME,
					game.rand().nextInt(Game.WIDTH),
					game.rand().nextInt(Game.HEIGHT),
					game.rand().nextInt(SML_STARS_TYPES),
					SML_STARS_ROWS, SML_STARS_COLS);
		}
	}
	
	public void update(){
		for(int i=0;i<BIG_STARS_COUNT;i++){
			big_stars[i].translate(speed * 4, 0);
			if(big_stars[i].getX() > Game.WIDTH + 10) {
				big_stars[i].moveto(-10, game.rand().nextInt(Game.HEIGHT));
				big_stars[i].changeFrame(game.rand().nextInt(BIG_STARS_TYPES));
			}
		}
		for(int i=0;i<MED_STARS_COUNT;i++){
			med_stars[i].translate(speed * 2, 0);
			if(med_stars[i].getX() > Game.WIDTH + 10) {
				med_stars[i].moveto(-10, game.rand().nextInt(Game.HEIGHT));
				med_stars[i].changeFrame(game.rand().nextInt(MED_STARS_TYPES));
			}
		}
		for(int i=0;i<SML_STARS_COUNT;i++){
			sml_stars[i].translate(speed, 0);
			if(sml_stars[i].getX() > Game.WIDTH + 10) {
				sml_stars[i].moveto(-10, game.rand().nextInt(Game.HEIGHT));
				sml_stars[i].changeFrame(game.rand().nextInt(SML_STARS_TYPES));
			}
		}
	}
	
	public void render() throws IOException{
		for(int i=0;i<BIG_STARS_COUNT;i++){
			big_stars[i].render();
		}
		for(int i=0;i<MED_STARS_COUNT;i++){
			med_stars[i].render();
		}
		for(int i=0;i<SML_STARS_COUNT;i++){
			sml_stars[i].render();
		}
	}
}
